package com.witframework.plugin.lucene.index;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.lucene.document.Document;

import com.witframework.plugin.lucene.IndexException;

public class DefaultResourceProcessor extends AbstractResourceProcessor {

	private List<Document> buffer=new ArrayList<Document>(64);
	private int maxBufferSize=64;
	@Override
	public void addDocumentInternal(Document document) {
		if(buffer.size()>maxBufferSize)
			flush();
		buffer.add(document);
	}
	/**
	 * 将缓存的Document对象产生索引。同时从缓存移除已处理的Document对象。
	 */
	public void flush() throws IndexException{
		for(Iterator<Document> itr = buffer.iterator(); itr.hasNext();){
			try {
				indexWriter.addDocument(itr.next());
			}  catch (Exception e) {
				throw new IndexException("cannot write indexes.",e);
			}
			itr.remove();
		}
	}

}
